home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS05.ADF / IFF / showilbm.c < prev    next >
C/C++ Source or Header  |  1986-04-20  |  8KB  |  253 lines

  1.  
  2. /** ShowILBM.c **************************************************************
  3.  *
  4.  * Read an ILBM raster image file and display it.      24-Jan-86.
  5.  *
  6.  * By Jerry Morrison, Steve Shaw, and Steve Hayes, Electronic Arts.
  7.  * This software is in the public domain.
  8.  *
  9.  * USE THIS AS AN EXAMPLE PROGRAM FOR AN IFF READER.
  10.  *
  11.  * The IFF reader portion is essentially a recursive-descent parser.
  12.  * The display portion is specific to the Commodore Amiga computer.
  13.  *
  14.  * NOTE: This program displays an image, pauses, then exits.
  15.  *
  16.  * Usage from CLI:
  17.  *   showilbm picture1 [picture2] ...
  18.  *
  19.  * Usage from WorkBench:
  20.  * Click on ShowILBM, hold down shift key, click on each picture to show,
  21.  * Double-click on final picture to complete the selection, release the
  22.  * shift key.
  23.  *
  24.  ****************************************************************************/
  25.  
  26. /* If you are constructing a Makefile, here are the names of the files 
  27.  * that you'll need to compile and link with to use showilbm:
  28.  
  29.      showilbm.c
  30.      readpict.c
  31.      remalloc.c
  32.      ilbmr.c
  33.      iffr.c
  34.      unpacker.c
  35.      gio.c
  36.  
  37.      and you'll have to get movmem() from lc.lib
  38.  
  39.  * robp.
  40.  * ********************************************************************** */
  41.  
  42.  
  43. #include "iff/intuall.h"
  44. #include "libraries/dos.h"
  45. #include "libraries/dosextens.h"
  46. #include "iff/ilbm.h"
  47. #include "workbench/workbench.h"
  48. #include "workbench/startup.h"
  49. #include "iff/readpict.h"
  50. #include "iff/remalloc.h"
  51.  
  52. #define LOCAL static
  53.  
  54. #define MIN(a,b) ((a)<(b)?(a):(b))
  55. #define MAX(a,b) ((a)>(b)?(a):(b))
  56.  
  57. /* general usage pointers */
  58. struct GfxBase *GfxBase;
  59. LONG IconBase; /* Actually, "struct IconBase *" if you've got some ".h" file*/
  60.  
  61. /* For displaying an image */
  62. LOCAL struct RastPort rP;
  63. LOCAL struct BitMap bitmap0;
  64. LOCAL struct RasInfo rasinfo;
  65. LOCAL struct View v = {0};
  66. LOCAL struct ViewPort vp = {0};
  67.  
  68. LOCAL ILBMFrame iFrame;
  69.     
  70. /* Define the size of a temporary buffer used in unscrambling the ILBM rows.*/
  71. #define bufSz 512
  72.  
  73. /* Message strings for IFFP codes. */
  74. LOCAL char MsgOkay[]        = {
  75.      "(IFF_OKAY) Didn't find a FORM ILBM in the file." };
  76. LOCAL char MsgEndMark[]     = { "(END_MARK) How did you get this message?" };
  77. LOCAL char MsgDone[]        = { "(IFF_DONE) All done."};
  78. LOCAL char MsgDos[]         = { "(DOS_ERROR) The DOS returned an error." };
  79. LOCAL char MsgNot[]         = { "(NOT_IFF) Not an IFF file." };
  80. LOCAL char MsgNoFile[]      = { "(NO_FILE) No such file found." };
  81. LOCAL char MsgClientError[] = {
  82.      "(CLIENT_ERROR) ShowILBM bug or insufficient RAM."};
  83. LOCAL char MsgForm[]        = { "(BAD_FORM) A malformed FORM ILBM." };
  84. LOCAL char MsgShort[]       = { "(SHORT_CHUNK) A malformed FORM ILBM." };
  85. LOCAL char MsgBad[]         = { "(BAD_IFF) A mangled IFF file." };
  86.  
  87. /* THESE MUST APPEAR IN RIGHT ORDER!! */
  88. LOCAL char *IFFPMessages[-(int)LAST_ERROR+1] = {
  89.     /*IFF_OKAY*/  MsgOkay,
  90.     /*END_MARK*/  MsgEndMark,
  91.     /*IFF_DONE*/  MsgDone,
  92.     /*DOS_ERROR*/ MsgDos,
  93.     /*NOT_IFF*/   MsgNot,
  94.     /*NO_FILE*/   MsgNoFile,
  95.     /*CLIENT_ERROR*/ MsgClientError,
  96.     /*BAD_FORM*/  MsgForm,
  97.     /*SHORT_CHUNK*/  MsgShort,
  98.     /*BAD_IFF*/   MsgBad
  99.     };
  100.  
  101. /** DisplayPic() ************************************************************
  102.  *
  103.  * Interface to Amiga graphics ROM routines.
  104.  *
  105.  ****************************************************************************/
  106. DisplayPic(bm, ptilbmFrame)
  107.     struct BitMap *bm;  ILBMFrame *ptilbmFrame;  {
  108.     int i;
  109.     struct View *oldView = GfxBase->ActiView;     /* so we can restore it */
  110.  
  111.     InitView(&v);
  112.     InitVPort(&vp);
  113.     v.ViewPort = &vp;
  114.     InitRastPort(&rP);
  115.     rP.BitMap = bm;
  116.     rasinfo.BitMap = bm;
  117.  
  118.     /* Always show the upper left-hand corner of this picture. */
  119.     rasinfo.RxOffset = 0;
  120.     rasinfo.RyOffset = 0;
  121.  
  122.     vp.DWidth = MAX(ptilbmFrame->bmHdr.w, 4*8);
  123.     vp.DHeight = ptilbmFrame->bmHdr.h;
  124.  
  125. #if 0
  126.     /* Specify where on screen to put the ViewPort. */
  127.     vp.DxOffset = ptilbmFrame->bmHdr.x;
  128.     vp.DyOffset = ptilbmFrame->bmHdr.y;
  129. #else
  130.     /* Always display it in upper left corner of screen.*/
  131. #endif
  132.  
  133.     if (ptilbmFrame->bmHdr.pageWidth <= 320) 
  134.      vp.Modes = 0;
  135.     else vp.Modes = HIRES;
  136.     if (ptilbmFrame->bmHdr.pageHeight > 200) {
  137.      v.Modes |= LACE;
  138.      vp.Modes |= LACE;
  139.      }
  140.     vp.RasInfo = &rasinfo;
  141.     MakeVPort(&v,&vp);
  142.     MrgCop(&v);
  143.     LoadView(&v);   /* show the picture */
  144.     WaitBlit();
  145.     WaitTOF();
  146.     LoadRGB4(&vp, ptilbmFrame->colorMap, ptilbmFrame->nColorRegs);
  147.  
  148.     for (i = 0; i < 5*60; ++i)  WaitTOF();   /* Delay 5 seconds. */
  149.  
  150.     LoadView(oldView);   /* switch back to old view */
  151.     }
  152.  
  153. /** stuff for main0() *******************************************************/
  154. LOCAL struct WBStartup *wbStartup = 0;  /* 0 unless started from WorkBench.*/
  155.  
  156. PrintS(msg)  char *msg; {
  157.     if (!wbStartup)  printf(msg);
  158.     }
  159.  
  160. void GoodBye(msg)  char *msg; {
  161.     PrintS(msg);   PrintS("\n");
  162.     exit(0);
  163.     }
  164.  
  165. /** OpenArg() ***************************************************************
  166.  *  Given a "workbench argument" (a file reference) and an I/O mode.
  167.  *  It opens the file.
  168.  ****************************************************************************/
  169. LONG OpenArg(wa, openmode)  struct WBArg *wa;   int openmode; {
  170.     LONG olddir;
  171.     LONG file;
  172.     if (wa->wa_Lock)   olddir = CurrentDir(wa->wa_Lock);
  173.     file = Open(wa->wa_Name, openmode);
  174.     if (wa->wa_Lock)   CurrentDir(olddir);
  175.     return(file);
  176.     }
  177.  
  178. /** main0() *****************************************************************/
  179. void main0(wa)  struct WBArg *wa;  {
  180.     LONG file;
  181.     IFFP iffp = NO_FILE;
  182.  
  183.     /* load and display the picture */
  184.     file = OpenArg(wa, MODE_OLDFILE);
  185.     if (file)
  186.      iffp = ReadPicture(file, &bitmap0, &iFrame, ChipAlloc);
  187.      /* Allocates BitMap using ChipAlloc().*/
  188.     Close(file);
  189.     if (iffp == IFF_DONE)
  190.      DisplayPic(&bitmap0, &iFrame);
  191.  
  192.     PrintS(" ");   PrintS(IFFPMessages[-iffp]);   PrintS("\n");
  193.  
  194.     /* cleanup */
  195.     if (bitmap0.Planes[0])  {
  196.      RemFree(bitmap0.Planes[0]);
  197.           /* ASSUMES allocated all planes via a single ChipAlloc call.*/
  198.      FreeVPortCopLists(&vp);
  199.      FreeCprList(v.LOFCprList);
  200.      }
  201.     }
  202.  
  203. /** main() ******************************************************************/
  204.  
  205. void main(argc, argv)  int argc;  char **argv;  {
  206.     struct WBArg wbArg, *wbArgs;
  207.     LONG olddir;
  208. /*sss    struct Process *myProcess; */
  209.  
  210.     if( !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)) )
  211.      GoodBye("No graphics.library");
  212.     if( !(IconBase = OpenLibrary("icon.library",0)) )
  213.      GoodBye("No icon.library");
  214.     if (!argc) {
  215.      /* Invoked via workbench */
  216.      wbStartup = (struct WBStartup *)argv;
  217.      wbArgs = wbStartup->sm_ArgList;
  218.      argc = wbStartup->sm_NumArgs;
  219.      while (argc >= 2) {
  220.          olddir = CurrentDir(wbArgs[1].wa_Lock);
  221.          main0(&wbArgs[1]);
  222.          argc--;   wbArgs = &wbArgs[1];
  223.          }
  224. #if 0
  225.      /* [TBD] We want to get an error msg to the Workbench user... */
  226.      if (argc < 2) {
  227.          PrintS ("Usage from workbench:\n");
  228.          PrintS (" Click mouse on Show-ILBM, Then hold 'SHIFT' key\n");
  229.          GoodBye(" while double-click on file to display.");
  230.          }
  231. #endif
  232.      }
  233.     else {
  234.      /* Invoked via CLI.  Make a lock for current directory.
  235.       * Eventually, scan name, separate out directory reference?*/
  236.      if (argc < 2)
  237.          GoodBye("Usage from CLI: 'Show-ILBM filename'");
  238. /*sss     myProcess = (struct Process *)FindTask(0); */
  239.      wbArg.wa_Lock = 0; /*sss myProcess->pr_CurrentDir; */
  240.      while (argc >= 2) {
  241.          wbArg.wa_Name = argv[1];
  242.          PrintS("Showing file ");   PrintS(wbArg.wa_Name);   PrintS(" ...");
  243.          main0(&wbArg);
  244.          PrintS("\n");
  245.          argc--;   argv = &argv[1];
  246.          }
  247.      }
  248.     CloseLibrary(GfxBase);
  249.     CloseLibrary(IconBase);
  250.     exit(0);
  251.     }
  252.  
  253.